

import java.io.IOException;
import java.net.URISyntaxException;
import java.sql.SQLException;
import java.util.logging.FileHandler;
import java.util.logging.Formatter;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.clsfd.database.DatabaseConnection;
import com.clsfd.database.SystemParams;
import com.clsfd.ivr.OutDialer;
import com.clsfd.sms.SMSHandler;
import com.logging.DcLogFormatter;

/**
 * Servlet implementation class receiver
 */
@WebServlet("/receiver")
public class receiver extends HttpServlet {
	private static final long serialVersionUID = 1L;
    private Logger Log=null;
    private DatabaseConnection Databaseconn= null;
	private SMSHandler SmsHandler = null;
    private SystemParams SysParams=null;
    public receiver() {
        super();
        try{
        	
        	Databaseconn = new DatabaseConnection(null);
        	SysParams = Databaseconn.getSystemParameters();
        	SmsHandler = new SMSHandler(SysParams);    		
	        FileHandler filehandler = new FileHandler(SysParams.getSmsHandlerLogFile()+"%g.log",1000000000,3,true);
			Formatter formatterTxt=new DcLogFormatter();
			filehandler.setFormatter(formatterTxt);
			Log = Logger.getLogger("SmsHandler");
			Log.addHandler(filehandler);
			Log.setLevel(Level.parse(SysParams.getSmsHandlerLogLevel()));
        }catch(Exception e){
        	e.printStackTrace();
        }
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			
		
			String SenderNumber="";
			String ReceiverNumber="";   
			String TextMessage="";
			
			SenderNumber=request.getParameter("sender").replace("+", "");	//%p   %q
			ReceiverNumber=request.getParameter("receiver");		//%P	%Q   
			
			TextMessage=request.getParameter("msg");		//%a
			String ServiceType="";
			String Operation="";
			
			Log.info("SMS Received from :"+SenderNumber+" To:"+ReceiverNumber+" MSG:"+TextMessage);
			
			
			/*
			TextMessage=TextMessage.replaceAll("[-+.^:,]", " ");		// replay all special cgars by space
			TextMessage=TextMessage.replaceAll("\\s+", " ");
			TextMessage=TextMessage.toLowerCase();
			
			if REG V <Correct Model> Subscribe it 
			 *if REG L <Correct Area> Subscribe it  
			 * 
			
			
			if (TextMessage.contains("buy")){
				Operation="buy";
			}
			else if (TextMessage.contains("sell")){
				Operation="sell";
			}
			
			if (TextMessage.contains("land"))
			{
				ServiceType="lands";
			}
			else if (TextMessage.contains("vehicle"))
			{
				ServiceType="vehicles";
			}
			
			*/
			OutDialer aaa= new OutDialer(SysParams);			
			aaa.setSearchParams(SenderNumber, "289", "classified.php", "out", SenderNumber, ServiceType, Operation, TextMessage);
			aaa.start();
		}catch(Exception e){
			Log.severe("Exception Occured :"+e.getMessage());
		}
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet( request,  response);
	}

}
